﻿using System.Linq;
using System.Threading.Tasks;

using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

using SeriesManager.Model;
using SeriesManager.API;
using System.Runtime.Serialization;
using WinRTFramework.Common;
using WinRTFramework.Interfaces.AppStates;

namespace SeriesManager.ViewModel
{
    public class SearchViewModel : ViewModelBase, ISupportTransientState
    {
        #region Properties

        private bool loading;
        private string name;

        public ICollectionView View
        { 
            get; 
            private set;
        }

        public bool Loading
        {
            get 
            { 
                return this.loading; 
            }
            private set 
            { 
                base.SetProperty<bool>(ref this.loading, value); 
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            private set
            {
                base.SetProperty<string>(ref this.name, value);
            }
        }

        #endregion

        #region DelegateCommands

        public DelegateCommand<ItemClickEventArgs> ItemClickedCommand 
        { 
            get; 
            private set; 
        }

        #endregion

        public SearchViewModel()
        {
            this.ItemClickedCommand = new DelegateCommand<ItemClickEventArgs>(OnItemClickExecuted);
        }

        #region DelegateCommandsImplementation

        private async void OnItemClickExecuted(ItemClickEventArgs args)
        {
            var clickedItem = (SeriesModel)args.ClickedItem;

            this.Loading = true;
            var fanart = clickedItem.FanArt;
            if (fanart != null)
            {
                await fanart.DownloadImage();
            }
            this.Loading = false;

            AppX.Instance.Current = clickedItem;
            base.NavigationService.Navigate<SeriesViewModel>(clickedItem.ID);
        }

        #endregion

        public override async void OnNavigatedTo(object navigationParameter)
        {
            base.OnNavigatedTo(navigationParameter);

            this.Name = (string)navigationParameter;
            this.Loading = true;

            var searchResult = await AppX.Instance.API.Search(this.Name, AppX.Instance.Language);
            foreach (var result in searchResult)
            {
                if (!AppX.Instance.Series.Any(S => S.ID == result.ID))
                {
                    AppX.Instance.Series.Add(result);
                }
            }

            this.View = new CollectionViewSource() { Source = searchResult }.View;
            base.OnPropertyChanged<ICollectionView>(() => this.View);

            this.Loading = false;
        }

        public void UpdateTransientState(object state)
        {
            var series = this.View.FirstOrDefault(S => (S as SeriesModel).ID == (int)state);
            if (series != null)
            {
                this.View.MoveCurrentTo(series);
            }
        }

        public object GetTransientState()
        {
            if (this.View != null && this.View.CurrentItem != null)
            {
                return (this.View.CurrentItem as SeriesModel).ID;
            }
            return null;
        }

        public System.Collections.Generic.IEnumerable<System.Type> GetTransientStateTypes()
        {
            yield return typeof(int);
        }
    }
}
